import Server from '@/config/server';
import request from '@/utils/request';
import router from '@/router';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  addEquipmentId: '',
  dataLists: [],
  Fields: [],
  itemDetail: {},
  equipmentData: [],
  groupEquipmentData: [],
  mapLists: [],
  isOnLineData: [],
};
const actions = {
  // 下载统计报表
  exportData({ commit }, data) {
    const params = JSON.parse(JSON.stringify(data));
    return request.get(Server.action.exportYieldExport, { params, responseType: 'blob' });
  },
  // 下载设备统计报表
  equipmentExportData({ commit }, data) {
    const params = JSON.parse(JSON.stringify(data));
    return request.get(Server.action.equipmentExportData, { params, responseType: 'blob' });
  },
  // 列表
  getLists({ commit }, page) {
    request.get(Server.action.equipment_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setLists', response.data.result.list);
          const { fields } = response.data.result;
          fields.push({
            title: '操作',
            align: 'center',
            width: 170,
            scopedSlots: { customRender: 'action' },
            fixed: 'right',
          });
          fields.forEach((item, i) => {
            fields[i].dataIndex = fields[i].key;
            fields[i].align = 'center';
            if (i < fields.length - 2) {
              fields[i].width = item.width || 180;
            }
          });
          fields[0].fixed = 'left';
          fields[0].width = 80;
          fields[0].scopedSlots = { customRender: fields[0].key };
          fields[1].width = 40;
          // fields[2].width = 100
          fields[3].width = 110;
          commit('setFields', fields);
        } else {

        }
      });
  },
  // 添加
  dataAdd({ commit }, page) {
    commit('setErrorCode', false);
    // 颗粒化判断先不写了，下面是对应的参数，预留
    // this.formItem.group_id == '' && typeof this.formItem.group_id == 'undefined'
    // this.formItem.name == ''
    // this.formItem.describe == ''
    // this.formItem.tunnel_abroad_long == ''
    // this.formItem.tunnel_abroad_width == ''
    // this.formItem.tunnel_abroad_height == ''
    // this.formItem.tunnel_long == ''
    // this.formItem.carry_width == ''
    // this.formItem.carry_height == ''
    // this.formItem.board_specs == ''
    // this.formItem.board_put == ''
    // this.formItem.trolley_long == ''
    // this.formItem.trolley_num == ''
    // this.formItem.kiln_volume == ''
    // this.formItem.trolley_volume == ''
    // this.formItem.img_pic == ''
    let flag = true;
    for (const key in page) {
      if (page[key]) {

      } else {
       	flag = false;
      }
    }
    if (!flag) {
      showError('所有输入框必填，请检查红色*必填项。');
      return false;
    }
    request.post(Server.action.equipment_add, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        commit('setAddEquipmentId', response.data.message);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
          router.push({
            query: {
              tabsKey: 2,
              equipment_id: response.data.message,
            },
          });
        } else if (response.status === 422) {
          showErrors(response.data.errors);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 编辑
  dataEdit({ commit }, page) {
    // console.log(page)
    commit('setErrorCode', false);
    let flag = true;
    for (const key in page) {
      if (page[key]) {

      } else {
       	flag = false;
      }
    }
    if (!flag) {
      showError('所有输入框必填，请检查红色*必填项。');
      return false;
    }
    request.post(Server.action.equipment_update, page)
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
          router.push({
            query: {
              tabsKey: 2,
              equipment_id: page.id,
              edit: true,
            },
          });
        } else {
          showError(response.data.message);
        }
      });
  },
  // ID删除 > 参数为：url/id
  // dataDel({commit}, id) {
  //   request.get(Server.action.equipment_delete + id)
  //   .then(response => {
  //     console.log(response);
  //     if (response.data.errorCode === 0) {
  //       showSuccess(response.data.result)
  //     } else {
  //       showError( response.data.message )
  //     }
  //   })
  // },
  // 批量删除 > 参数为：[1,2,3,4,5,6]
  dataDel({ commit }, ids) {
    request.get(Server.action.equipment_delete, { params: ids })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 详情
  dataDetail({ commit }, id) {
    request.get(Server.action.equipment_detail + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setDetail', response.data.result);
        // showSuccess(response.data.result)
        } else {
          showError(response.data.message);
        }
      });
  },
  // 设备名称信息
  equipmentAll({ commit }) {
    request.get(Server.action.equipment_all)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setEquipmentAll', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 根据公司查询设备
  groupEquipmentAll({ commit }, id) {
    request.get(Server.action.equipment_groupEquipment, { params: id })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setGroupEquipmentAll', response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 设备地图展示
  mapLists({ commit }) {
    request.get(Server.action.Map_list)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setMapLists', response.data.result.list);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 设备在线状态
  isOnLine({ commit }) {
    request.get(Server.action.equipment_isOnLine)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          const data = response.data.result;
          const statusData = {
            onLine: 0,
            offLine: 0,
          };
          data.forEach((item) => {
            if (item.status == '1') {
              statusData.onLine = item.counts;
            } else if (item.status == '0') {
              statusData.offLine = item.counts;
            }
          });
          console.log(statusData);
          commit('setisOnLine', statusData);
        } else {
        // showError( response.data.message )
        }
      });
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setLists(state, lists) {
	    state.dataLists = lists;
  },
  setFields(state, lists) {
	    state.Fields = lists;
  },
  setDetail(state, data) {
    state.itemDetail = data;
  },
  setAddEquipmentId(state, data) {
    state.addEquipmentId = data;
  },
  setEquipmentAll(state, data) {
    state.equipmentData = data;
  },
  setGroupEquipmentAll(state, data) {
    state.groupEquipmentData = data;
  },
  setMapLists(state, lists) {
    state.mapLists = lists;
  },
  setisOnLine(state, data) {
    state.isOnLineData = data;
  },
};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
